package com.op.studyingliteapp.fragment.notif;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.op.studyingliteapp.R;

/**
 * 
 * @author handsome
 *
 */
public class NotifListAdapter extends BaseAdapter {

	public ArrayList<NotifCellData> list;
	public Context mContext;
	private DisplayImageOptions options;

	public NotifListAdapter(Context mContext, ArrayList<NotifCellData> list) {
		super();
		this.list = list;
		this.mContext = mContext;
		options = new DisplayImageOptions.Builder().showImageOnLoading(R.drawable.ic_stub).showImageForEmptyUri(R.drawable.ic_empty).showImageOnFail(R.drawable.ic_error).cacheInMemory(true).cacheOnDisk(true).considerExifParams(true).bitmapConfig(Bitmap.Config.RGB_565).build();
	}

	public Context getContext() {
		return this.mContext;
	}
	

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public NotifCellData getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LinearLayout fl = null;
		if (convertView != null) {
			fl = (LinearLayout) convertView;
		} else {
			fl = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.item_notif, parent, false);
		}
		NotifCellData data = getItem(position);

		ImageView notifView = (ImageView) fl.findViewById(R.id.notif_image);
		TextView notifName = (TextView) fl.findViewById(R.id.notif_name);
		TextView title = (TextView) fl.findViewById(R.id.notif_title);
		TextView notifCotext = (TextView) fl.findViewById(R.id.notif_context);
		TextView notifTime = (TextView) fl.findViewById(R.id.notif_time);
		// 头像设置
		ImageLoader.getInstance().displayImage(data.getImage(), notifView, options);
		notifName.setText(data.getName());
		title.setText(data.getTitle());
		if (data.getContext().length() > 20) {
			notifCotext.setText(data.getContext().substring(0, 20));
		} else {
			notifCotext.setText(data.getContext());
		}
		notifTime.setText(data.getTime());

		return fl;
	}

}
